package com.lesson01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalc{
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame {
    //属性
    TextField t1,t2,t3;
    //方法
    public void loadFrame(){
        //组件：
        //3个文本框
        t1 = new TextField(10);
        t2 = new TextField(10);
        t3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new CalculatorListener(this));
        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());
        add(t1);
        add(label);
        add(t2);
        add(button);
        add(t3);

        pack();
        setVisible(true);
    }
    public Calculator() {
        loadFrame();
    }
}
//监听器类
class CalculatorListener implements ActionListener {
    //获取变量 组合
    Calculator calculator = null;
    public CalculatorListener(Calculator calculator)
    {
        this.calculator = calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得数字
        int n1 = Integer.parseInt(calculator.t1.getText());
        int n2 = Integer.parseInt(calculator.t2.getText());
        //运算放回
        calculator.t3.setText(""+(n1+n2));
        //清除前面
        calculator.t1.setText("");
        calculator.t2.setText("");
    }
}